package com.army.study.provider;

import com.army.study.ResourceTable;
import com.army.study.data.DeviceData;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

public class DeviceDataProvider extends ListComponentProvider<DeviceData> {

    /**
     * 实例化新的设备数据提供程序。
     * @param context
     * @param listBean
     * @param resourceId
     */
    public DeviceDataProvider(Context context, List<DeviceData> listBean, int resourceId) {
        super(context, listBean, resourceId);
    }

    /**
     * 数据项绑定
     * @param componentViewHolder
     * @param deviceData
     * @param position
     */
    @Override
    public void onItemDataBind(ComponentViewHolder componentViewHolder, DeviceData deviceData, int position) {
        // 1 设置设备的名称
        Text deviceText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_item_image_title);
        deviceText.setText(deviceData.getDeviceInfo().getDeviceName());
        Text infoText = (Text) componentViewHolder.getChildComponent(ResourceTable.Id_item_image_info);
        infoText.setText(deviceData.getDeviceInfo().getDeviceId());

        // 2 设置设备的图标
        setDeviceImg(componentViewHolder, deviceData);
    }


    private void setDeviceImg(ComponentViewHolder componentViewHolder, DeviceData deviceData) {
        int imageTypeId = -1;
        switch (deviceData.getDeviceInfo().getDeviceType()) {
            case SMART_PHONE:
                imageTypeId = ResourceTable.Media_dv_phone;
                break;
            case SMART_PAD:
                imageTypeId = ResourceTable.Media_dv_pad;
                break;
            case SMART_WATCH:
                imageTypeId = ResourceTable.Media_dv_watch;
                break;
            default:
                break;
        }
        if (imageTypeId != -1) {
            Image image = (Image) componentViewHolder.getChildComponent(ResourceTable.Id_item_type);
            image.setPixelMap(imageTypeId);
        }
    }

    @Override
    public void onItemClick(Component component, DeviceData item, int position) {
        for (DeviceData deviceData : listBean) {
            deviceData.setChecked(false);
        }
        listBean.get(position).setChecked(true);

        // 通知数据有修改
        notifyDataChanged();
    }
}
